/*
* @Author: dengjiayao
* @Date:   2017-08-28 17:19:29
 * @Last Modified by: songwh
 * @Last Modified time: 2018-08-17 10:14:39
*/
import { modal } from 'vue-strap'
import { showMsg, searchSecurity, addKeyEvents, updateKeyEvents, eventKpiList, dependValueDescList} from '../../../vuex/action'
import DatePicker from 'vue-datepicker'

import getDatePickerOption from '../../../util/date-picker-option'

// 节流函数
const delay = (function() {
  let timer = 0
  return function(callback, ms) {
    clearTimeout(timer)
    timer = setTimeout(callback, ms)
  }
})()

export default {
  data() {
    return {
      showModal: false,
      showStockList: false,
      keyword: '',
      actionType: 'ADD', // ADD or UPDATE
      form: {
        eventCategory: '2',
        eventType:'',
        eventDesc: '',
        startDt: '',
        endDt: '',
        kpiProcStatus: '',
        eventLink: ''
      },
      codeList: [],
      typeList: [],
      currentCode: {},
      eventKpis: [],
      datePickerOption: getDatePickerOption({
        format: 'YYYY-MM-DD HH:mm:ss'
      })
    }
  },
  components: {
    'vue-modal': modal,
    'vue-date-picker': DatePicker
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xSearchSecurity: searchSecurity,
      xAddKeyEvents: addKeyEvents,
      xUpdateKeyEvents: updateKeyEvents,
      xEventKpiList: eventKpiList,
      xDependValueDescList: dependValueDescList
    }
  },
  ready() {
    this.getSelectList()
  },
  methods: {
    getKpiList(eventId){
      this.xEventKpiList({
        eventId: eventId
      })
        .then((res) => {
          this.eventKpis = res.list || []
        })
    },
    valid() {
      let fields = ['eventCategory', 'eventType', 'startDt', 'eventDesc']
      for (let k of fields) {
        let v = this.form[k] + ''
        if (v.length === 0) {
          return Promise.reject({
            error_msg: '*标识的均为必填项'
          })
        }
      }
      
      if (!this.eventKpis || this.eventKpis.length ===0) {
        return Promise.reject({
          error_msg: '*标识的均为必填项'
        })
      }
      console.log((new Date(this.form.startDt)).getTime())
      console.log((new Date()).getTime())
      if((new Date(this.form.startDt)).getTime() > (new Date()).getTime()){
        return Promise.reject({
          error_msg: '开始时间不能大于当前时间'
        })
      }
      return Promise.resolve()
    },
    getSelectList(){
      this.xDependValueDescList({
        columName: 'event_type',
        dependValue: '2' 
      })
        .then((res) => {
          this.typeList = res.list
        })
    },
    searchStock(val){
      if(!val){
        return
      } else {
        this.xSearchSecurity({
          keyword: val
        })
          .then((res) => {
            this.codeList = res.list
            this.showStockList = true
          })
      }
    },
    selectCode(it){
      this.keyword = `${it.secuCode}  ${it.secuName}`
      this.currentCode = it
      this.showStockList = false
    },
    submit() {
      this.valid()
        .then(() => {
          if(this.actionType === 'ADD'){
            this.addKey()
          } else {
            this.updateKey()
          }
        })
        .catch(res => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    cancelFun(eventId){
      this.showModal = false
    },
    addKey(){
      let params = {
        eventCategory: this.form.eventCategory,
        eventType: this.form.eventType,
        eventDesc: this.form.eventDesc,
        startDt: new Date(this.form.startDt).getTime(),
        endDt: new Date(this.form.startDt).getTime(),
        kpiProcStatus: this.form.kpiProcStatus,
        eventLink: this.form.eventLink,
        eventKpis: this.eventKpis
      }
      this.xAddKeyEvents(params)
        .then((res) => {
          this.showModal = false
        })
    },
    updateKey(){
      let params = {
        eventId: this.form.eventId,
        eventCategory: this.form.eventCategory,
        eventType: this.form.eventType,
        eventDesc: this.form.eventDesc,
        startDt: new Date(this.form.startDt).getTime(),
        endDt: new Date(this.form.startDt).getTime(),
        kpiProcStatus: this.form.kpiProcStatus,
        eventLink: this.form.eventLink,
        eventKpis: this.eventKpis
      }
      this.xUpdateKeyEvents(params)
        .then((res) => {
          this.showModal = false
        })
    },
    addCode() {
      if(this.isExist(this.currentCode.secuId) || !this.currentCode.secuId) return 
      this.eventKpis.push({
        secuCode: this.currentCode.secuCode,
        kpiValue: '0',
        secuName: this.currentCode.secuName,
        secuId: this.currentCode.secuId
      })
      this.currentCode = {}
    },
    isExist(secuId){
      let exist = false
      for(let i in this.eventKpis){
        if(i.secuId === secuId) exist = true
      }
      return exist
    },
    deleteCode(it){
      for(let i=0; i< this.eventKpis.length; i ++){
        if(this.eventKpis[i].secuId === it.secuId){
          this.eventKpis.splice(i, 1)
        }
      }
    },
    validKpiValue(val){
      console.log(val)
    }
  },
  watch: {
    keyword(val) {
      if(val.length > 6) return
      delay(() => {
        this.searchStock(val)
      }, 300)
    }
  },
}
